import config from "./config.js"

const httpPost = (url, data,auth) => {
	auth = uni.getStorageSync("token");
    let httpDefaultOpts = {
        url: config.api_url+url,
        data: data,
        beforeSend :function(xmlHttp){
            xmlHttp.setRequestHeader("If-Modified-Since","0"); 
            xmlHttp.setRequestHeader("Cache-Control","no-cache");
        },
        method: "POST",
        header: {
		   'content-type': 'application/x-www-form-urlencoded',
		   "Authorization":auth?auth:''
		},
        dataType: 'json',
    }
    let promise = new Promise(function(resolve, reject) {
        uni.request(httpDefaultOpts).then(
            (res) => {
                resolve(res[1])
            }
        ).catch(
            (response) => {
                resolve(response[1])
            }
        )
    })
    return promise
};
const httpGet = (url, data,auth) => {
    let httpDefaultOpts = {
        url: config.api_url+url,
        data: data,
        beforeSend :function(xmlHttp){
            xmlHttp.setRequestHeader("If-Modified-Since","0"); 
            xmlHttp.setRequestHeader("Cache-Control","no-cache");
        },
        method: "GET",
        header: {
			'X-Requested-With': 'XMLHttpRequest',
			"Accept": "application/json",
			"Content-Type": "application/json; charset=UTF-8",
			"Authorization":auth?auth:''
		},
        dataType: 'json',
    }
    let promise = new Promise(function(resolve, reject) {
        uni.request(httpDefaultOpts).then(
            (res) => {
                resolve(res[1])
            }
        ).catch(
            (response) => {
                resolve(response[1])
            }
        )
    })
    return promise
};
export default {
    httpPost,
	httpGet
}